package com.net.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;

import com.net.dao.SuperDao;
import com.net.model.Customer;
import com.net.service.CustomerService;
import com.net.util.HibernateUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("Customer-*")
@Scope("prototype")
public class CustomerAction extends ActionSupport {
	private Customer customer;
	private CustomerService customerService;
	private List<Customer> customers;
	private String[] selectedList;
	private String cid;
	private String orderBy;
	private String keyword;

	public String add() throws Exception {
		// System.out.println(customer.getCustomer_name()+"llllll");
		if (selectedList != null) {
			for (int i = 0; i < selectedList.length; i++) {
				System.out.println(Integer.parseInt(selectedList[i]));
				if (customers.get(Integer.parseInt(selectedList[i]))
						.getCustomer_name().equals("")) {

				} else {
					this.customerService.add(customers.get(Integer
							.parseInt(selectedList[i])));
				}
			}
		}
		this.list();
		return "add";
	}

	public String delete() throws Exception {
		this.customerService.delete(customer);
		this.list();
		// System.out.println(customer.getId()+":"+customer.getCustomer_name());
		return "delete";
	}

	public String deleteSelected() throws Exception {
		// System.out.println(selectedList.length+"  :8888888888888888");
		Customer cust = new Customer();
		if (selectedList != null) {
			for (int i = 0; i < selectedList.length; i++) {
				System.out.println(Integer.parseInt(selectedList[i]));
				cust.setId(Integer.parseInt(selectedList[i]));
				this.customerService.delete(cust);
			}
		}
		this.list();
		return "deleteSelected";
	}

	public String list() throws Exception {
		this.customers = customerService.getCostomers();
		// System.out.println(customers.get(0).getShort_name()+"++++++++++++++");
		return "list";
	}

	public String load() {
		this.customer = this.customerService.loadCustomerById(customer.getId());
		return "load";
	}

	public String update() {
		// System.out.println(customer.getId()+"#################");
		customer = customerService.loadCustomerById(customer.getId());
		// System.out.println(customer.getId()+"#################");
		return "update";
	}

	public String updateCommit() throws Exception {
		// System.out.println(customer.getId()+"*****##########");
		customerService.update(customer);
		this.list();
		return "updateCommit";
	}

	public String search() {
		this.customers = customerService.searchList(cid, keyword, orderBy);
		return "search";
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	@Resource(name = "customerService")
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	public String[] getSelectedList() {
		return selectedList;
	}

	public void setSelectedList(String[] selectedList) {
		this.selectedList = selectedList;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

}
